<template>
  <div>
    <el-card style="margin-bottom: 10px">
      <div v-if="!flag">
        <span>选择考评：</span>
        <el-select v-model="value" placeholder="选择指标" style="margin-right: 5px">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div v-if="flag"><span>没有正在进行的考评</span></div>
    </el-card>
    <el-card>
      <el-collapse v-for="item in tableData">
        <el-collapse-item :title="item.content">
          <el-collapse v-for="items in item.child">
            <el-collapse-item :title="items.content" style="margin-left: 30px">
              <el-collapse v-for="content in items.child" style="margin-left: 30px">
                <el-collapse-item :title="content.content">
                  <div>
                    <div style="display: flex;">
                      <span style="margin-right: 20px">佐证材料(点击标签查看)：</span>
                      <el-upload
                          class="upload-demo"
                          :action="$baseUrl + '/file/upload'"
                          :headers="{token: user.token}"
                          :show-file-list="false"
                          :on-success="
                                  (response, file, fileList) => {
                                    return fileUpload(response, file, fileList, content)
                                  }
                               ">
                        <el-button size="small" type="primary" style="height: 20px; line-height: 3px">上传</el-button>
                      </el-upload>
                    </div>
                    <el-tag
                        :key="tag.id"
                        v-for="tag in content.files"
                        closable
                        :disable-transitions="false"
                        @close="handleClose(tag)" style="margin-right: 5px; margin-top: 5px;cursor: pointer"
                        @click="fileView(tag.path)">
                      <a style="text-decoration: none; color: #001529;cursor: pointer">{{ tag.name }}</a>
                    </el-tag>
                  </div>
                  <el-form>
                    <el-form-item inline>
                      <div style="display: flex; justify-content: flex-end; margin-right: 30px">
                        <el-input style="width: 90px;margin-right: 10px;" placeholder="自评"
                                  v-model="records.score"></el-input>
                        <div>
                          <el-button type="primary" @click="updateRecord(content)">保存</el-button>
                        </div>
                      </div>
                    </el-form-item>
                  </el-form>
                </el-collapse-item>
              </el-collapse>
            </el-collapse-item>
          </el-collapse>
        </el-collapse-item>
      </el-collapse>
    </el-card>
  </div>
</template>

<script>
import request from "@/urils/request";
import {Base64} from "js-base64";

export default {
  name: "GschoolInput",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user') || '{}'),
      tableData: [],
      options: [],
      value: 1,
      flag: false,
      fileList: [],
      records: {
        criterionId: '',
        schoolId: JSON.parse(localStorage.getItem("user")).id,
        score: ''
      },
      newFile: {
        name: '',
        path: '',
        criterionId: '',
        userId: ''
      },
    }
  },
  methods: {
    getData(value) {
      request.get('/criterion/selectTree/' + value + '/' + this.user.id).then(res => {
        if (res.code === '200') {
          this.tableData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    loadTemplate() {
      request.get('/evaluation/getActive').then(res => {
        if (res.code === '200') {
          if (res.data.length === 0) {
            this.flag = true
          } else {
            this.flag = false
            for (let i in res.data) {
              this.options.push({
                value: res.data[i].templateId,
                label: res.data[i].name
              })
            }
          }
        }
      })
    },
    handleClose(file) {
      request.post('/file/delete', file).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.getData(this.value)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    fileView(path) {
      var fileName = path.split("/").pop()
      console.log(fileName)
      // var previewUrl = 'http://124.222.214.139:9090/file/download/6c68b1299b714e99a22a3fbf336a28aa.jpg' + '&fullfilename=' + '6c68b1299b714e99a22a3fbf336a28aa.jpg'
      // var previewUrl = 'http://124.222.214.139:9090/file/download/6c68b1299b714e99a22a3fbf336a28aa.jpg' + '&fullfilename=' + '6c68b1299b714e99a22a3fbf336a28aa.jpg'
      var previewUrl = path + '&fullfilename=' + fileName
      window.open("http://124.222.214.139:8012/onlinePreview?url=" + encodeURIComponent(Base64.encode(previewUrl)));
      // window.open('http://124.222.214.139:8012/onlinePreview?url=' + encodeURIComponent(Base64.encode(path)));
    },
    fileUpload(response, file, fileList, content) {
      if (response.code === '200') {
        this.newFile.name = file.name
        this.newFile.path = response.data
        this.newFile.criterionId = content.id
        this.newFile.userId = this.user.id
        request.post("/file/addFile", this.newFile).then(res => {
          if (res.code === '200') {
            this.$message.success("上传成功")
            this.getData(this.value)
          } else {
            this.$message.error(res.msg)
          }
        })
      }else{
        this.$message.error(response.msg)
      }

    },
    updateRecord(content) {
      this.records.criterionId = content.id
      request.post('/evaluation/updateRecord', this.records).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.records.score = null
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  },
  created() {
    this.getData(1)
    this.loadTemplate()
  },
  watch: {
    value: function (newVal, oldVal) {
      this.getData(newVal)
    }
  }
}
</script>

<style scoped>

</style>